package lily.phs.vaccination;
import lily.phs.object.HereTipa;
import lily.phs.object.VaccinationCard;
import lily.phs.urh.R;
import lily.ui.control.MyPopupWindow;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.InputType;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;

public class VaccinationCardView extends RelativeLayout implements OnClickListener,OnFocusChangeListener {
    /** Called when the activity is first created. */
	private EditText et_profile_id,et_vaca_name,et_vaca_sex,et_vaca_born,et_vaca_nation,
			 et_vaca_number_name,et_vaca_child_id,et_vaca_born_id,et_vaca_guardian_name,
			 et_vaca_relation,et_vaca_guardian_phone,et_vaca_home_address_county,
			 et_vaca_home_address_village,et_vaca_census_address_province,et_vaca_census_address_city,
			 et_vaca_census_address_county,et_vaca_census_address_village,et_vaca_immigration_time,et_vaca_emigration_time,
			 et_vaca_emigration_reason,et_vaca_vaccine_anomalies_his,et_vaca_contraindication,
			 et_vaca_infect_his,et_vaca_create_card_date,et_vaca_create_card_people;
	private RadioButton rb_vaca_census_address1,rb_vaca_census_address2;
	private RadioGroup rg_vaca_census_address;
	private int profileId=0;
	HereTipa ht;
	public VaccinationCard vaca;
	MyPopupWindow mPopupWindow;
	private Context myContext;
	View v;
	Paint paint;
	private LinearLayout ll;
	public VaccinationCardView(Context context ,int pro) {
		super(context);
		// TODO Auto-generated constructor stub
		myContext=context;
		this.profileId=pro;
		paint=new Paint();
		paint.setStyle(Paint.Style.FILL_AND_STROKE);
		paint.setTypeface(Typeface.DEFAULT_BOLD);
		paint.setColor(Color.BLACK);	
		final LayoutInflater inflater = (LayoutInflater)myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		v=inflater.inflate(R.layout.vaccination_card, null);
		findWidget();
		addToView();
	}
	/**
	 * 将整个界面作为一个view对象
	 * @return 整个界面view
	 */
//	public View getMyView(){
//		return this.v;
//	}
	
	 class MyView extends View{
		 View v;
		 Paint paint;
		 public MyView(Context context){
			 super(context);
			 paint=new Paint();
			 paint.setStyle(Paint.Style.FILL_AND_STROKE);
			 paint.setTypeface(Typeface.DEFAULT_BOLD);
			 paint.setColor(Color.BLACK);
		 }
		 public void onDraw(Canvas c){
			 int top=ll.getTop();
			 int left=ll.getLeft()+30;
			 int right=ll.getRight()+30;
			 int y=0;
			 top=top-y;
		 //  c.drawLine(left, top, right, top, paint);
		//	 c.drawLine(left, top+500, right, top+500, paint);	 
		//	 c.drawLine(left, top, left, top+500, paint);
		//	 c.drawLine(right, top, right, top+500, paint);
		 }
	}
	 public void addToView(){
		 LayoutParams lp=new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
			this.addView(v,lp);
			MyView mv=new MyView(myContext);
			this.addView(mv,lp);
	 }
	 public void findWidget(){
//		 LayoutInflater factory=LayoutInflater.from(myContext);
//		 v=factory.inflate(R.layout.vaccination_card, null);
		 ll=(LinearLayout)v.findViewById(R.id.fam_eco_hushuxing);
		 et_profile_id=(EditText)v.findViewById(R.id.et_vaca_id);
		 et_vaca_name=(EditText)v.findViewById(R.id.et_vaca_name);
		 et_vaca_sex=(EditText)v.findViewById(R.id.et_vaca_sex);
		 et_vaca_born=(EditText)v.findViewById(R.id.et_vaca_born);
		 et_vaca_born.setInputType(InputType.TYPE_NULL);
		 et_vaca_born.setOnClickListener(this);
		 et_vaca_born.setOnFocusChangeListener(this);
		 et_vaca_nation=(EditText)v.findViewById(R.id.et_vaca_nation);
		 et_vaca_number_name=(EditText)v.findViewById(R.id.et_vaca_number_name);
		 et_vaca_child_id=(EditText)v.findViewById(R.id.et_vaca_child_id);
		 et_vaca_born_id=(EditText)v.findViewById(R.id.et_vaca_born_id);
		 et_vaca_guardian_name=(EditText)v.findViewById(R.id.et_vaca_guardian_name);
		 et_vaca_relation=(EditText)v.findViewById(R.id.et_vaca_relation);
		 et_vaca_guardian_phone=(EditText)v.findViewById(R.id.et_vaca_guardian_phone);
		 et_vaca_home_address_county=(EditText)v.findViewById(R.id.et_vaca_home_address_county);
		 et_vaca_home_address_village=(EditText)v.findViewById(R.id.et_vaca_home_address_village);
		 et_vaca_census_address_province=(EditText)v.findViewById(R.id.et_vaca_census_address_province);
		 et_vaca_census_address_city=(EditText)v.findViewById(R.id.et_vaca_census_address_city);
		 et_vaca_census_address_county=(EditText)v.findViewById(R.id.et_vaca_census_address_county);
		 et_vaca_census_address_village=(EditText)v.findViewById(R.id.et_vaca_census_address_village);
		 et_vaca_immigration_time=(EditText)v.findViewById(R.id.et_vaca_immigration_time);
		 et_vaca_immigration_time.setInputType(InputType.TYPE_NULL);
		 et_vaca_immigration_time.setOnClickListener(this);
		 et_vaca_immigration_time.setOnFocusChangeListener(this);
		 et_vaca_emigration_time=(EditText)v.findViewById(R.id.et_vaca_emigration_time);
		 et_vaca_emigration_time.setInputType(InputType.TYPE_NULL);
		 et_vaca_emigration_time.setOnClickListener(this);
		 et_vaca_emigration_time.setOnFocusChangeListener(this);
		 et_vaca_emigration_reason=(EditText)v.findViewById(R.id.et_vaca_emigration_reason);
		 et_vaca_vaccine_anomalies_his=(EditText)v.findViewById(R.id.et_vaca_vaccine_anomalies_his);
		 et_vaca_contraindication=(EditText)v.findViewById(R.id.et_vaca_contraindication);
		 et_vaca_infect_his=(EditText)v.findViewById(R.id.et_vaca_infect_his);
		 et_vaca_create_card_date=(EditText)v.findViewById(R.id.et_vaca_create_card_date);
		 et_vaca_create_card_date.setInputType(InputType.TYPE_NULL);
		 et_vaca_create_card_date.setOnClickListener(this);
		 et_vaca_create_card_date.setOnFocusChangeListener(this);
		 et_vaca_create_card_people=(EditText)v.findViewById(R.id.et_vaca_create_card_people);
		 rg_vaca_census_address=(RadioGroup)v.findViewById(R.id.rg_vaca_census_address);
		 rb_vaca_census_address1=(RadioButton)v.findViewById(R.id.rb_vaca_census_address1);
		 rb_vaca_census_address2=(RadioButton)v.findViewById(R.id.rb_vaca_census_address2);
		 rb_vaca_census_address1.setOnClickListener( this);
		 rb_vaca_census_address2.setOnClickListener(this);
	 }
	 
	 
	 public void selectData(){
		 ht=new HereTipa(myContext);
		 ht.selectNameAndCode(profileId);
		 vaca=new VaccinationCard(myContext);
		 vaca.selectVaccinationCard(profileId);
	 }
	 
	 
	 
	 public void onFocusChange(View v, boolean hasFocus) {
			// TODO Auto-generated method stub
			if(v==et_vaca_born&&hasFocus){
				mPopupWindow = new MyPopupWindow(getContext());			
				mPopupWindow.showAtLocation(findViewById(R.id.et_vaca_born), Gravity.CENTER, 0, 0);

				Button btn=mPopupWindow.getButton();
				btn.setOnClickListener(new OnClickListener(){

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						et_vaca_born.setText(mPopupWindow.getDate());
						mPopupWindow.dismiss();
					}
					
				});
			}
			else if(v==et_vaca_immigration_time&&hasFocus){
				mPopupWindow = new MyPopupWindow(getContext());			
				mPopupWindow.showAtLocation(findViewById(R.id.et_vaca_immigration_time), Gravity.CENTER, 0, 0);

				Button btn=mPopupWindow.getButton();
				btn.setOnClickListener(new OnClickListener(){

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						et_vaca_immigration_time.setText(mPopupWindow.getDate());
						mPopupWindow.dismiss();
					}
					
				});
			}
			else if(v==et_vaca_emigration_time&&hasFocus){
				mPopupWindow = new MyPopupWindow(getContext());			
				mPopupWindow.showAtLocation(findViewById(R.id.et_vaca_emigration_time), Gravity.CENTER, 0, 0);

				Button btn=mPopupWindow.getButton();
				btn.setOnClickListener(new OnClickListener(){

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						et_vaca_emigration_time.setText(mPopupWindow.getDate());
						mPopupWindow.dismiss();
					}
					
				});
			}
			else if(v==et_vaca_create_card_date&&hasFocus){
				mPopupWindow = new MyPopupWindow(getContext());			
				mPopupWindow.showAtLocation(findViewById(R.id.et_vaca_create_card_date), Gravity.CENTER, 0, 0);

				Button btn=mPopupWindow.getButton();
				btn.setOnClickListener(new OnClickListener(){

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						et_vaca_create_card_date.setText(mPopupWindow.getDate());
						mPopupWindow.dismiss();
					}
					
				});
			}
	    }
	 
	 
	 
	 
	 
	 
	 public void onClick(View v){
		 if(v==rb_vaca_census_address1){
			 
		 }
		 else if(v==rb_vaca_census_address2){
			 if(rb_vaca_census_address2.isChecked()){
				 et_vaca_census_address_province.requestFocus();
				// et_vaca_census_address_city
				// et_vaca_census_address_county
				// et_vaca_census_address_village
			 }
			 else {
				 et_vaca_census_address_province.setEnabled(false);
				 et_vaca_census_address_city.setEnabled(false);
				 et_vaca_census_address_county.setEnabled(false);
				 et_vaca_census_address_village.setEnabled(false);
			 }
		 }
		 else if(v==et_vaca_born){
			 mPopupWindow = new MyPopupWindow(myContext);			
				mPopupWindow.showAtLocation(v.findViewById(R.id.et_vaca_born), Gravity.CENTER, 0, 0);

				Button btn=mPopupWindow.getButton();
				btn.setOnClickListener(new OnClickListener(){

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						et_vaca_born.setText(mPopupWindow.getDate());
						mPopupWindow.dismiss();
					}
					
				});
		 }
		 else if(v==et_vaca_emigration_time){
			 mPopupWindow = new MyPopupWindow(myContext);			
				mPopupWindow.showAtLocation(v.findViewById(R.id.et_vaca_emigration_time), Gravity.CENTER, 0, 0);

				Button btn=mPopupWindow.getButton();
				btn.setOnClickListener(new OnClickListener(){

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						et_vaca_emigration_time.setText(mPopupWindow.getDate());
						mPopupWindow.dismiss();
					}
					
				});
		 }
		 else if(v==et_vaca_immigration_time){
			 mPopupWindow = new MyPopupWindow(myContext);			
				mPopupWindow.showAtLocation(v.findViewById(R.id.et_vaca_immigration_time), Gravity.CENTER, 0, 0);

				Button btn=mPopupWindow.getButton();
				btn.setOnClickListener(new OnClickListener(){

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						et_vaca_immigration_time.setText(mPopupWindow.getDate());
						mPopupWindow.dismiss();
					}
					
				});
		 }
		 else if(v==et_vaca_create_card_date){
			 mPopupWindow = new MyPopupWindow(myContext);			
				mPopupWindow.showAtLocation(v.findViewById(R.id.et_vaca_create_card_date), Gravity.CENTER, 0, 0);

				Button btn=mPopupWindow.getButton();
				btn.setOnClickListener(new OnClickListener(){

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						et_vaca_create_card_date.setText(mPopupWindow.getDate());
						mPopupWindow.dismiss();
					}
					
				});
		 }
	 }
	 
	 /**初始化预防接种卡
	  * 
	  */
	 public void initVaccinationCard(){
		// HereTipa ht=new HereTipa(myContext);
		// ht.selectNameAndCode(profileId);
		 if(ht.getHeti_name()!=null){
			 et_vaca_name.setText(ht.getHeti_name());
		 }
		 if(ht.getHeti_here_id()!=null){
			 et_profile_id.setText(ht.getHeti_here_id());
		 }
		 if(vaca.getVaca_sex()!=null){
			 et_vaca_sex.setText(vaca.getVaca_sex());
		 }
		 if(vaca.getVaca_born()!=null){
			 et_vaca_born.setText(vaca.getVaca_born());
		 }
		 if(vaca.getVaca_nation()!=null){
			 et_vaca_nation.setText(vaca.getVaca_nation());
		 }
		 if(vaca.getVaca_number_name()!=null){
			 et_vaca_number_name.setText(vaca.getVaca_number_name());
		 }
		 if(vaca.getVaca_child_id()!=null){
			 et_vaca_child_id.setText(vaca.getVaca_child_id());
		 }
		 if(vaca.getVaca_child_born_id()!=null){
			 et_vaca_born_id.setText(vaca.getVaca_child_born_id());
		 }
		 if(vaca.getVaca_guardian_name()!=null){
			 et_vaca_guardian_name.setText(vaca.getVaca_guardian_name());
		 }
		 if(vaca.getVaca_guardian_phone()!=null){
			 et_vaca_guardian_phone.setText(vaca.getVaca_guardian_phone());
		 }
		 if(vaca.getVaca_relation()!=null){
			 et_vaca_relation.setText(vaca.getVaca_relation());
		 }
		 if(vaca.getVaca_home_address()!=null&&vaca.getVaca_home_address().length()>0){
			 String []arrHomeAddress=vaca.getVaca_home_address().split(",");
			 if(arrHomeAddress!=null&&arrHomeAddress.length>0){
				 for(int i=0;i<arrHomeAddress.length;i++){
					 if(i==0)et_vaca_home_address_county.setText(arrHomeAddress[i]);
					 if(i==1)et_vaca_home_address_village.setText(arrHomeAddress[i]);
				 }
			 }
			 
		 }
		 if(vaca.getVaca_census_address()>=0){
			 if(vaca.getVaca_census_address()==0){
				 rg_vaca_census_address.check(0);
				 rb_vaca_census_address1.setChecked(true);
			 }
			 else if(vaca.getVaca_census_address()==1){
				 rg_vaca_census_address.check(1);
				 rb_vaca_census_address2.setChecked(true);
			 }
		 }
		 if(vaca.getVaca_census_address2()!=null&&vaca.getVaca_census_address2().length()>0){
			 String []arrCensusAddress=vaca.getVaca_census_address2().split(",");
			 if(arrCensusAddress!=null&&arrCensusAddress.length>0){
				 for(int i=0;i<arrCensusAddress.length;i++){
					 if(i==0)et_vaca_census_address_province.setText(arrCensusAddress[i]);
					 if(i==1)et_vaca_census_address_city.setText(arrCensusAddress[i]);
					 if(i==2) et_vaca_census_address_county.setText(arrCensusAddress[i]);
					 if(i==3)et_vaca_census_address_village.setText(arrCensusAddress[i]);
				 }
			 }
			 
		 }
		 if(vaca.getVaca_immigration_time()!=null){
			 et_vaca_immigration_time.setText(vaca.getVaca_immigration_time());
		 }
		 if(vaca.getVaca_emigration_time()!=null){
			 et_vaca_emigration_time.setText(vaca.getVaca_emigration_time());
		 }
		 if(vaca.getVaca_emigration_reason()!=null){
			 et_vaca_emigration_reason.setText(vaca.getVaca_emigration_reason());
		 }
		 if(vaca.getVaca_vaccine_anomalies_his()!=null){
			 et_vaca_vaccine_anomalies_his.setText(vaca.getVaca_vaccine_anomalies_his());
		 }
		 if(vaca.getVaca_contraindication()!=null){
			 et_vaca_contraindication.setText(vaca.getVaca_contraindication());
		 }
		 if(vaca.getVaca_infect_his()!=null){
			 et_vaca_infect_his.setText(vaca.getVaca_infect_his());
		 }
		 if(vaca.getVaca_create_card_date()!=null){
			 et_vaca_create_card_date.setText(vaca.getVaca_create_card_date());
		 }
		 if(vaca.getVaca_create_card_people()!=null){
			 et_vaca_create_card_people.setText(vaca.getVaca_create_card_people());
		 }	 
	 }
	 
	 /**保存预防接种卡信息
	  * 
	  */
	 public boolean saveVaccinationCard(){
		 VaccinationCard vc=new VaccinationCard(myContext);
		 vc.selectVaccinationCard(profileId);
		 vaca.setVaca_sex(et_vaca_sex.getText().toString());
		 vaca.setVaca_born(et_vaca_born.getText().toString());
		 vaca.setVaca_nation(et_vaca_nation.getText().toString());
		 vaca.setVaca_number_name(et_vaca_number_name.getText().toString());
		 vaca.setVaca_child_id(et_vaca_child_id.getText().toString());
		 vaca.setVaca_child_born_id(et_vaca_born_id.getText().toString());
		 vaca.setVaca_guardian_name(et_vaca_guardian_name.getText().toString());
		 vaca.setVaca_relation(et_vaca_relation.getText().toString());
		 vaca.setVaca_guardian_phone(et_vaca_guardian_phone.getText().toString());
		 vaca.setVaca_home_address(et_vaca_home_address_county.getText().toString()+","+
				 et_vaca_home_address_village.getText().toString());
		 vaca.setVaca_census_address2(et_vaca_census_address_province.getText().toString()+"," +
				 et_vaca_census_address_city.getText().toString()+","+
				 et_vaca_census_address_county.getText().toString()+","+
				 et_vaca_census_address_village.getText().toString());
		 if(rb_vaca_census_address1.isChecked()){
			 
			 vaca.setVaca_census_address(0);
		 }
		 else if(rb_vaca_census_address2.isChecked()){
			 vaca.setVaca_census_address(1);
		 }
		 vaca.setVaca_immigration_time(et_vaca_immigration_time.getText().toString());
		 vaca.setVaca_emigration_time(et_vaca_emigration_time.getText().toString());
		 vaca.setVaca_emigration_reason(et_vaca_emigration_reason.getText().toString());
		 vaca.setVaca_vaccine_anomalies_his(et_vaca_vaccine_anomalies_his.getText().toString());
		 vaca.setVaca_contraindication(et_vaca_contraindication.getText().toString());
		 vaca.setVaca_infect_his(et_vaca_infect_his.getText().toString());
		 vaca.setVaca_create_card_date(et_vaca_create_card_date.getText().toString());
		 vaca.setVaca_create_card_people(et_vaca_create_card_people.getText().toString());
		 vaca.updateVaccinationCard(profileId);
		 return true;
	 }
	
  
}
